<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.gradeName" placeholder="全部年级" clearable style="width: 120px" class="filter-item" size="mini" round @change="handleFilter">
        <el-option v-for="item in gradeoption" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-input v-model="listQuery.keyword" placeholder="请输入关键字" style="width: 200px;margin-left: 10px" class="filter-item" size="mini" round @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" size="mini" style="margin-left: 10px" round @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" size="mini" style="margin-left: 10px;float:right" @click="handleCreate">
        保存
      </el-button>
      <el-button class="filter-item" size="mini" style="margin-left: 10px;float:right" @click="rrr">
        返回
      </el-button>

    </div>

    <el-table
      ref="table"
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      stripe
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @selection-change="selectRow"
    >
      <el-table-column type="selection" width="77" />
      <el-table-column label="序号" align="center" width="160px">
        <template slot-scope="{$index}">
          <span>{{ $index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年级" width="600px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gradeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级" width="600px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.className }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { getclasswithoutTeacherList, addclassTeacherList, addList } from '@/api/classSetting'
import { getall } from '@/api/grade'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      sels: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        // title: undefined,
        gradeName: '',
        keyword: '',
        teacherId: ''
      },
      gradeoption: [],
      temp: [{
        classId: '',
        gradeId: '',
        teacherId: ''
      }],
      dialogFormVisible: false,
      dialogStatus: '',
      dialogPvVisible: false,
      pvData: [],
      rules: {
        // class: [{ required: true, message: 'class is required', trigger: 'blur' }],
        // grade: [{ required: true, message: 'grade is required', trigger: 'blur' }],
        // person: [{ required: true, message: 'person is required', trigger: 'blur' }],
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
    getall().then(res => {
      console.log(res.data[0])
      for (let i = 0; i < res.data.length; i++) {
        this.gradeoption.push({ value: `${res.data[i]}`, label: `${res.data[i]}` })
      }
    })
  },
  methods: {
    rrr() {
      this.$router.push('/setting/index/5/classSetting')
    },

    selectRow(sels) {
      this.sels = sels
      console.log(this.sels)
    },
    getList() {
      this.listQuery.teacherId = 'fxz'
      getclasswithoutTeacherList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      for (let i = 0; i < this.sels.length; i++) {
        this.temp[i].classId = this.sels[i].classId
        this.temp[i].gradeId = this.sels[i].gradeId
        this.temp[i].teacherId = 'fxz'
        console.log(this.temp[i])
        addList(this.temp[i]).then(res => {
          console.log(res)
        })
      }
      console.log(this.temp)
      this.$notify({
        title: 'Success',
        message: 'Created Successfully',
        type: 'success',
        duration: 2000
      })
      this.rrr()
    }

  }
}
</script>
